package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.constants.FxfzConstants;
import com.css.fxfzaqbz.modules.data.entity.AuxiliaryMeasures;
import com.css.fxfzaqbz.modules.data.repository.AuxiliaryMeasuresRepository;
import com.css.fxfzaqbz.modules.data.service.AuxiliaryMeasuresService;
import com.css.fxfzaqbz.util.CreateGson;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import com.css.fxfzaqbz.zcpt.sys.service.SysAreaService;
import com.css.fxfzaqbz.zcpt.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AuxiliaryMeasuresServiceImpl implements AuxiliaryMeasuresService {
    @Resource
    SUserService sUserService;
    @Autowired
    SysAreaService sysAreaService;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    AuxiliaryMeasuresRepository auxiliaryMeasuresRepository;
    @Override
    public Map<String, Object> getAllAuxiliaryMeasures(AuxiliaryMeasures auxiliaryMeasures) {
        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        String provinceId = sUser.getProvinceId();
        String userId = PlatformSessionContext.getUserID();
        String role = sysRoleService.getRoleByUserId(userId);
        List<Map> maps = JSONObject.parseArray(role, Map.class);//一个人可能有多个角色
        //当前登录人所有的角色
        StringBuilder roles = new StringBuilder();
        for (int i = 0; i < maps.size(); i++) {
            Map map = maps.get(i);
            roles.append(map.get("roleCode") + ",");
        }
        String allRole = roles.toString();

        //如果所有角色包括省级管理员
        if (allRole.contains(FxfzConstants.AQBZ_SJGLY_ROLE)) {

            if (PlatformObjectUtils.isEmpty(sUser.getProvinceId())) {
                return null;
            }
            auxiliaryMeasures.setProvinceId(provinceId);
        }
        //如果所有角色包括国家级管理员
        if (allRole.contains(FxfzConstants.AQBZ_GJJGLY_ROLE)) {
            auxiliaryMeasures.setProvinceId("");
        }
        //如果所有角色包括活动管理员
        if (allRole.contains(FxfzConstants.AQBZ_HDGLY_ROLE)) {
            auxiliaryMeasures.setDlrId(userId);
        }
        //如果所有角色包括业务人员
        if (allRole.contains(FxfzConstants.AQBZ_YERY_ROLE)) {
            return null;
        }
        return auxiliaryMeasuresRepository.getAllAuxiliaryMeasures(auxiliaryMeasures);
    }

    @Override
    public void updateBigStatus(String id) {
        auxiliaryMeasuresRepository.updateBigStatus(id);
    }
}
